import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait  #等待模块
from selenium.webdriver.support import expected_conditions as ec  #条件模块
import pytest


class DXM:
    def browser(self):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        return driver

    def asser(self, driver, result, expect):
        if result == expect:
            print("success")
        else:
            driver.save_screenshot("error.jpg")
            print("failed")
        assert result == expect

    def login(self, driver, login_name='lisi', login_password='123456'):
        driver.find_element(By.ID, "username").send_keys(login_name)
        driver.find_element(By.ID, 'password').send_keys(login_password)
        time.sleep(2)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(2)
        result = driver.title
        return result

    def card_add(self, driver, context_bank, context_card_id):
        time.sleep(3)

        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(2)
        driver.find_element(By.ID, 'bankCard_add_modal_btn').click()
        time.sleep(2)
        driver.find_element(By.ID, 'cardBank_add_input').send_keys(context_bank)
        time.sleep(2)
        driver.find_element(By.XPATH,
                            '//*[@id="bankCardAddModal"]/div/div/div[2]/form/div[2]/div[1]/div/label[1]').click()
        time.sleep(2)
        driver.find_element(By.ID, 'cardNum_add_input').send_keys(context_card_id)
        driver.find_element(By.ID, 'bankCard_save_btn').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(2)

        result = driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/div/h4").text
        # expect = "伊朗中央银行 (借记卡)"
        # result1 = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[3]/div/div/div[2]/h4[1]').text
        # expect1 = "卡号：1234567891548796"
        return result

    def card_del(self, driver):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[2]").click()
        time.sleep(2)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()

    def regist(self, driver, regist_name='wangwu', regist_password='123456', regist_repassword='123456'):
        driver.find_element(By.PARTIAL_LINK_TEXT, "还没有账号？去注册").click()
        time.sleep(5)
        driver.find_element(By.ID, "username").send_keys(regist_name)
        driver.find_element(By.ID, 'password').send_keys(regist_password)
        driver.find_element(By.ID, "repassword").send_keys(regist_repassword)
        driver.find_element(By.ID, "login_btn").click()

    def loan(self, driver):
        time.sleep(3)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH,
                            '/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="password"]')
        time.sleep(2)
        # driver.find_element(By.XPATH, '//*[@id="password"]').send_keys('666666')
        time.sleep(2)
        driver.switch_to.default_content()
        driver.find_element(By.CLASS_NAME, "layui-layer-btn0").click()
        time.sleep(2)
        result = driver.find_element(By.XPATH, '//*[@id="jconfirm-box28971"]/div')
        return result

    def info(self, driver, info_money='10000', info_day='10'):
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(info_money)
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(info_day)
        time.sleep(2)
        driver.find_element(By.ID, 'submit').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)

    def shenhe(self):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        time.sleep(2)

        driver.find_element(By.ID, "username").send_keys('admin')
        driver.find_element(By.ID, 'password').send_keys('123456')
        driver.find_element(By.ID, "login_btn").click()

        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/ul/li[1]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH,
                            '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)

    def yanzheng(self, info_name='lisi', info_password='123456'):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()

        driver.find_element(By.ID, "username").send_keys(info_name)
        driver.find_element(By.ID, 'password').send_keys(info_password)
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(5)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/a').click()
        time.sleep(2)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]/a').click()
        time.sleep(2)
        result = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]').text
        return result

        #/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]
        # self.browser()
        # self.login(driver, 'admin', '123456')
        # time.sleep(5)
        # driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/a').click()
        # time.sleep(2)
        # driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/ul/li[1]/a').click()
        # time.sleep(2)
        # driver.find_element(By.XPATH,
        #                     '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]').click()
        # time.sleep(2)
        # driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()

    # def info_admin(self,driver):
